package com.yzt.loginmodule.service;

import android.app.IntentService;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Looper;
import android.text.TextUtils;

import androidx.annotation.Nullable;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 短信倒计时服务
 */
public class CountdownService extends IntentService {

    private CountDownTimer countDownTimer;

    private String mobile;

    private static final String TAG = CountdownService.class.getSimpleName();

    public CountdownService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (TextUtils.isEmpty(mobile) || !intent.getStringExtra("mobile").equals(mobile)) {
            mobile = intent.getStringExtra("mobile");
            if (countDownTimer != null) {
                countDownTimer.cancel();
            } else {
                countDownTimer = new CountDownTimer(60 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        if ((millisUntilFinished + 500) / 1000 != 0) {
                        }
                    }

                    @Override
                    public void onFinish() {
                        Looper.myLooper().quit();
                    }
                };
            }
            countDownTimer.start();
        }
        Looper.loop();
    }

    @Override
    public void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        super.onDestroy();
    }
}